--[[
	Lysidia's Titan Bag Total

	This is the main program segment.
]]

--------------------------------------------------------------------------------------------------
-- Program Global Variables
--------------------------------------------------------------------------------------------------
LBT_VERSION				= "0.85";


--------------------------------------------------------------------------------------------------
-- Generic program functions
--------------------------------------------------------------------------------------------------
-- Function to return the "button" text.
function LBT_GetButtonText()
	-- If no bag selected, return immediately.
	if (LBT_Config.BagSelected == nil) then
		return;
	end

	-- If we are displaying the bag name, do so here
	if (LBT_Config.DisplayBagName) then
		local bagName	= LysBagTotal_GetBagName(LBT_Config.BagSelected);
		return NORMAL_FONT_COLOR_CODE..bagName..FONT_COLOR_CODE_CLOSE..": "..LBT_BagValue(LBT_Config.BagSelected);
	end

	-- Otherwise, we just return the value for the bag
	return LBT_BagValue(LBT_Config.BagSelected);
end

function LysBagTotal_GetBagName(bagNum)
	local myBag = GetBagName(bagNum);

	if (myBag == nil) then
		myBag = "No Bag";
	end

	return myBag;
end

--------------------------------------------------------------------------------------------------
-- Button specific functions.
--------------------------------------------------------------------------------------------------
-- Function to change the displayed bag number
function LBT_ChangeDisplayBag( frame )
	local bagNum = frame.value;
	if (bagNum == nil) then return; end

	if (bagNum == 127) then
		return;
	end

	LBT_Config.BagSelected = bagNum;
end


--------------------------------------------------------------------------------------------------
-- Program functions below
--------------------------------------------------------------------------------------------------
function LBT_OnLoad( frame )
	if (LBT_Config == nil) then
		LBT_Config = {};
	end

	if (LBT_Config.BagSelected == nil) then
		LBT_Config.BagSelected = 0;
	end

	if (LBT_Config.DisplayBagName == nil) then
		LBT_Config.DisplayBagName = true;
	end

	-- Events to listen for
	frame:RegisterEvent("BAG_UPDATE");
	frame:RegisterEvent("UPDATE_INVENTORY_ALERTS");
end

-- Function: Main Event function handler
function LBT_OnEvent()
	-- Set the button text
	LBTBroker.text = LBT_GetButtonText();
end

-- Function: Set the tooltip text.
function LBT_SetTooltip(myTooltip)
	myTooltip:ClearLines()

	myTooltip:AddLine(LBT_TOOLTIP);
	
	-- Show tally for each bag seperately
	for j = 0, 4 do
		-- Check if the bag actually has any slots as we don't want to do cash tallies of bag slots with no bag in them.
		if (GetContainerNumSlots(j) > 0) then
			myTooltip:AddDoubleLine(NORMAL_FONT_COLOR_CODE..j.." - "..GetBagName(j)..FONT_COLOR_CODE_CLOSE, LBT_BagValue(j));
		else
			myTooltip:AddLine(NORMAL_FONT_COLOR_CODE.."No Bag"..FONT_COLOR_CODE_CLOSE);
		end
	end
	
	myTooltip:AddLine(" ");
	myTooltip:AddLine(GREEN_FONT_COLOR_CODE..LBT_HINT1..FONT_COLOR_CODE_CLOSE);
	myTooltip:AddLine(GREEN_FONT_COLOR_CODE..LBT_HINT2..FONT_COLOR_CODE_CLOSE);
end

-- Function: Select the next bag, rotating to bag 0 when we hit the last bag.
function LBT_SelectNextBag()
	-- Local Variables
	local bagSize;
	local bagOK = 0;

	while (bagOK == 0) do
		-- Rotate to the next bag
		LBT_Config.BagSelected = LBT_Config.BagSelected + 1;

		-- Rotate back to zero if this takes us past the last bag
		if (LBT_Config.BagSelected > 4) then
			LBT_Config.BagSelected = 0;

			-- Update the LDB Button
			LBT_OnEvent();

			return;
		end

		-- Okay, we weren't the last possible bag, therefore check the bag slots
		if (GetContainerNumSlots(LBT_Config.BagSelected) > 0) then
			bagOK = 1;

			-- Update the LDB Button
			LBT_OnEvent();

			return;
		end
	end
end
